﻿//C#
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class PlatformCollision : MonoBehaviour
{
    public float maxAngle = 45.0f;
	public bool isBottom;
	public Vector3 orientation = new Vector3(0,1,0);
	bool isLetDowned;
	float counter;
	public MeshCollider myMeshCol;
	public bool isBox = false;
	public bool isHidden = false;
	public bool isIgnore = false;
	
    void Start ()
    {
        float cos = Mathf.Cos(maxAngle);
        MeshFilter MF = GetComponent<MeshFilter>();
        myMeshCol = GetComponent<MeshCollider>();
        if (MF == null || myMeshCol == null || MF.sharedMesh == null)
        {
			Debug.Log(gameObject.name);
            Debug.LogError("PlatformCollision needs a MeshFilter and a MeshCollider");
            return;
        }
        Mesh M = new Mesh();
        Vector3[] verts = MF.sharedMesh.vertices;
        List<int> triangles = new List<int>(MF.sharedMesh.triangles);
        for (int i = triangles.Count-1; i >=0 ; i -= 3)
        {
            Vector3 P1 = transform.TransformPoint(verts[triangles[i-2]]);
            Vector3 P2 = transform.TransformPoint(verts[triangles[i-1]]);
            Vector3 P3 = transform.TransformPoint(verts[triangles[i  ]]);
            Vector3 faceNormal = Vector3.Cross(P3-P2,P1-P2).normalized;
			
            if (Vector3.Dot(faceNormal, orientation) <= cos)
            {
                triangles.RemoveAt(i);
                triangles.RemoveAt(i-1);
                triangles.RemoveAt(i-2);
            }
        }
        M.vertices = verts;
        M.triangles = triangles.ToArray();
        myMeshCol.sharedMesh = M;
    }

	void Update()
	{
		if(isLetDowned)
		{
			counter += Time.deltaTime;
			if(counter > 1)
			{
				isLetDowned  = false;
				myMeshCol.enabled = true;
			}
		}
	}
	
	public void letPlayerDown()
	{
		if(isBottom)
			return;
		isLetDowned = true;
		counter = 0;
		myMeshCol.enabled = false;
	}
}